/**
 * 
 */
package com.activities;


import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.Animations.AnimUtil;
import com.Data.Options;
import com.Data.Options.onPreferencesGetCompleted;
import com.Data.Options.onPreferencesUpdateCompleted;
import com.Managers.FragmentManagerClass;
import com.Managers.UserManagerClass;
import com.TextFormat.UtilFonts;
import com.shareup.R;


/**
 * @author Chris
 * 
 */
public class ActivityPreferences extends Activity {

    private CheckBox allow_follow;

    private CheckBox allow_share;

    private final OnClickListener backClick = new OnClickListener() {

        @Override
        public void onClick(View v) {

            exit();

        }
    };

    private CheckBox blur_cover;

    private boolean coverUpdated;

    private Options options;

    private boolean update;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            //
            return true;
        }
        //
        return super.onKeyUp(keyCode, event);
    }

    private boolean checkifwasChange() {

        boolean share = convertToBoolean(options.getAllowShare());
        boolean follow = convertToBoolean(options.getAllowFollow());
        boolean cover = convertToBoolean(options.getBlurCover());

        update = false;

        if (allow_share.isChecked() != share) {
            options.setAllowShare(convertToInt(allow_share.isChecked()));
            update = true;
        }

        if (allow_follow.isChecked() != follow) {
            options.setAllowFollow(convertToInt(allow_follow.isChecked()));
            update = true;
        }

        if (blur_cover.isChecked() != cover) {
            options.setBlurCover(convertToInt(blur_cover.isChecked()));
            update = true;
            coverUpdated = true;
        }

        return update;

    }

    private boolean convertToBoolean(int value) {

        return !(value == 0);
    }

    private int convertToInt(boolean value) {

        if (value)
            return 1;
        else
            return 0;
    }

    private void exit() {

        if (checkifwasChange())
            savePrefs();
        else {

            finish();
            AnimUtil.getActivityTransition(this, false);
        }

    }

    private void exitAfter() {

        if (coverUpdated)
            FragmentManagerClass.aProfile.downloadCover(UserManagerClass.UserLogged.getCoverPicture());
        finish();
        AnimUtil.getActivityTransition(this, false);
    }

    private void getPrefs() {

        Options opt = new Options(this);
        opt.setIdUser(UserManagerClass.UserLogged.getID());
        opt.setCallbackGet(new onPreferencesGetCompleted() {

            @Override
            public void Completed(Options result) {

                options = result;
                setPrefs(result);

            }
        });
        opt.execute(Options.ACTION_GET);
    }

    private void init() {

        allow_share = (CheckBox) findViewById(R.id.check_allowshare);
        allow_follow = (CheckBox) findViewById(R.id.Check_allowfollow);
        blur_cover = (CheckBox) findViewById(R.id.Check_blurcover);
        //

        ImageView backB = (ImageView) findViewById(R.id.button_back);
        backB.setOnClickListener(backClick);
        //
        UtilFonts.setTextBoldFont(((TextView) findViewById(R.id.title_actionbar)));
        UtilFonts.setTextBoldFont(((TextView) findViewById(R.id.title_1)));
        UtilFonts.setTextBoldFont(((TextView) findViewById(R.id.title_2)));
        UtilFonts.setTextRegularFont(((TextView) findViewById(R.id.t1)), ((TextView) findViewById(R.id.t2)), ((TextView) findViewById(R.id.tt)));

        getPrefs();

    }

    private void savePrefs() {

        Options opt = new Options(this);
        opt.setAllowFollow(options.getAllowFollow());
        opt.setAllowShare(options.getAllowShare());
        opt.setBlurCover(options.getBlurCover());

        opt.setCallbackUpdate(new onPreferencesUpdateCompleted() {

            @Override
            public void Completed(String result) {

                exitAfter();

            }
        });
        opt.setIdUser(UserManagerClass.UserLogged.getID());
        opt.execute(Options.ACTION_SAVE);
        UserManagerClass.UserLogged.setBlur(options.getBlurCover());
    }

    private void setPrefs(Options opt) {

        allow_follow.setChecked(convertToBoolean(opt.getAllowFollow()));
        allow_share.setChecked(convertToBoolean(opt.getAllowShare()));
        blur_cover.setChecked(convertToBoolean(opt.getBlurCover()));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setContentView(R.layout.prefs);
        super.onCreate(savedInstanceState);

        init();
    }
}
